﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Globalization;

namespace WPFStorageClient
{
    /// <summary>
    /// Interaction logic for CreateNewAccountInfoDialog.xaml
    /// </summary>
    public partial class EditAccountInfoDialog : Window, INotifyPropertyChanged
    {
        public EditAccountInfoDialog()
        {
            DataContext = this;
            InitializeComponent();
        }

        private String _accountName;
        public String AccountName
        {
            get
            {
                return _accountName;
            }
            set
            {
                bool fUpdate = false;
                if (String.IsNullOrEmpty(value))
                {
                    fUpdate = !String.IsNullOrEmpty(_accountName);
                }
                else if (String.IsNullOrEmpty(_accountName))
                {
                    fUpdate = true;
                }
                else
                {
                    fUpdate = (String.Compare(_accountName, value, true, CultureInfo.InstalledUICulture) != 0);
                }
                if (fUpdate)
                {
                    _accountName = value;
                    NotifyPropertyChanged("AccountName");
                }
            }
        }
        private String _sharedKey;
        public String AccountSharedKey
        {
            get
            {
                return _sharedKey;
            }
            set
            {
                bool fUpdate = false;
                if (String.IsNullOrEmpty(value))
                {
                    fUpdate = !String.IsNullOrEmpty(_sharedKey);
                }
                else if (String.IsNullOrEmpty(_sharedKey))
                {
                    fUpdate = true;
                }
                else
                {
                    fUpdate = (String.Compare(_sharedKey, value, true, CultureInfo.InstalledUICulture) != 0);
                }
                if (fUpdate)
                {
                    _sharedKey = value;
                    NotifyPropertyChanged("AccountSharedKey");
                }
            }
        }


        private bool _saveEnabled = false;
        public bool SaveEnabled
        {
            get
            {
                return _saveEnabled;
            }
            private set
            {
                if (value != _saveEnabled)
                {
                    _saveEnabled = value;
                    NotifyPropertyChanged("SaveEnabled");
                }
            }
        }
        #region PropertyChangeRegion
        // Implementation of Property change events.
        //
        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(String strPropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
            }
        }
        #endregion

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            SaveEnabled = _accountNameBox.Text.Length > 0 &&
                            _sharedKeyBox.Text.Length > 0;

        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

    }
}
